﻿using Lamex.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mIE.Calculate
{
    public class LGC_VAL_S02_S03
    {
        //dt必须排序-PART_NO | dt_Fabric、dt_Foam
        public static DataTable GetSeat_Value(string Part_No, string Feat_Name, out double Feat_Value)
        {
            Feat_Value = 0;
            DataTable dtResult = Common.GetTable.GetTable_SizeParmList_Seat_Value();
            DataTable dt_Fabric = BaseBuildSql.GetDataTableBySql(" select * from IE_Part_SizeParmList_Fabric where PART_NO='" + Part_No + "'");
            DataTable dt_Foam = BaseBuildSql.GetDataTableBySql(" select * from IE_Part_SizeParmList_Foam where PART_NO='" + Part_No + "'");
            double S02_CUT_LEATHER = 0, S02_CUT_FABRIC = 0, S02_SEW = 0, S02_CHECK_LEATHER = 0, S03_CUT_H = 0, S03_CUT_V = 0, S03_CUT_PUNCH = 0;
            double W = 0, L = 0;
            DataRow dr = dtResult.NewRow();
            for (int i = 0; i < dt_Fabric.Rows.Count; i++)
            {
                double REQUIRE_QTY = Convert.ToDouble(dt_Fabric.Rows[i]["REQUIRE_QTY"].ToString().Length > 0 ? dt_Fabric.Rows[i]["REQUIRE_QTY"].ToString() : "0");
                double FRONT_SIZE = Convert.ToDouble(dt_Fabric.Rows[i]["FRONT_SIZE"].ToString().Length > 0 ? dt_Fabric.Rows[i]["FRONT_SIZE"].ToString() : "0");
                double FRONT_QTY = Convert.ToDouble(dt_Fabric.Rows[i]["FRONT_QTY"].ToString().Length > 0 ? dt_Fabric.Rows[i]["FRONT_QTY"].ToString() : "0");
                double BACK_SIZE = Convert.ToDouble(dt_Fabric.Rows[i]["BACK_SIZE"].ToString().Length > 0 ? dt_Fabric.Rows[i]["BACK_SIZE"].ToString() : "0");
                double BACK_QTY = Convert.ToDouble(dt_Fabric.Rows[i]["BACK_QTY"].ToString().Length > 0 ? dt_Fabric.Rows[i]["BACK_QTY"].ToString() : "0");
                double LEFT_SIZE = Convert.ToDouble(dt_Fabric.Rows[i]["LEFT_SIZE"].ToString().Length > 0 ? dt_Fabric.Rows[i]["LEFT_SIZE"].ToString() : "0");
                double LEFT_QTY = Convert.ToDouble(dt_Fabric.Rows[i]["LEFT_QTY"].ToString().Length > 0 ? dt_Fabric.Rows[i]["LEFT_QTY"].ToString() : "0");
                double RIGHT_SIZE = Convert.ToDouble(dt_Fabric.Rows[i]["RIGHT_SIZE"].ToString().Length > 0 ? dt_Fabric.Rows[i]["RIGHT_SIZE"].ToString() : "0");
                double RIGHT_QTY = Convert.ToDouble(dt_Fabric.Rows[i]["RIGHT_QTY"].ToString().Length > 0 ? dt_Fabric.Rows[i]["RIGHT_QTY"].ToString() : "0");

                //S02_裁布真皮
                if (dt_Fabric.Rows[i]["FABRIC_TYPE"].ToString() == "真皮")
                    S02_CUT_LEATHER = S02_CUT_LEATHER + (FRONT_SIZE + BACK_SIZE + LEFT_SIZE + RIGHT_SIZE) * REQUIRE_QTY;
                //S02_裁布普通
                if (dt_Fabric.Rows[i]["FABRIC_TYPE"].ToString() == "布(含假皮及网布)")
                    S02_CUT_FABRIC = S02_CUT_FABRIC + (FRONT_SIZE + BACK_SIZE + LEFT_SIZE + RIGHT_SIZE) * REQUIRE_QTY;
                //S02_电车车缝
                // if (dt_Fabric.Rows[i]["FABRIC_TYPE"].ToString() != "绵")
                S02_SEW = S02_SEW + (FRONT_SIZE * FRONT_QTY + BACK_SIZE * BACK_QTY + LEFT_SIZE * LEFT_QTY + RIGHT_SIZE * RIGHT_QTY) * REQUIRE_QTY;
                //S02_真皮检查
                if (dt_Fabric.Rows[i]["FABRIC_TYPE"].ToString() == "真皮")
                    S02_CHECK_LEATHER = S02_CHECK_LEATHER + Maximum(FRONT_SIZE * LEFT_SIZE, FRONT_SIZE * RIGHT_SIZE, BACK_SIZE * LEFT_SIZE, BACK_SIZE * RIGHT_SIZE) * REQUIRE_QTY;

            }
            for (int j = 0; j < dt_Foam.Rows.Count; j++)
            {
                double REQUIRE_QTY = Convert.ToDouble(dt_Foam.Rows[j]["REQUIRE_QTY"].ToString().Length > 0 ? dt_Foam.Rows[j]["REQUIRE_QTY"].ToString() : "0");
                double SPONGE_LEN = Convert.ToDouble(dt_Foam.Rows[j]["FOAM_CUT_LEN"].ToString().Length > 0 ? dt_Foam.Rows[j]["FOAM_CUT_LEN"].ToString() : "0");
                double SPONGE_WID = Convert.ToDouble(dt_Foam.Rows[j]["FOAM_CUT_WIDTH"].ToString().Length > 0 ? dt_Foam.Rows[j]["FOAM_CUT_WIDTH"].ToString() : "0");
                double SPONGE_THI = Convert.ToDouble(dt_Foam.Rows[j]["FOAM_CUT_THICK"].ToString().Length > 0 ? dt_Foam.Rows[j]["FOAM_CUT_THICK"].ToString() : "0");

                //S03_平刀机
                if (dt_Foam.Rows[j]["FOAM_PROCESS"].ToString().Contains("平刀"))
                {
                    if (dt_Foam.Rows[j]["RAW_MAT_SIZE"].ToString().Length > 0)
                    {
                        string[] W_L = dt_Foam.Rows[j]["RAW_MAT_SIZE"].ToString().Split('*');
                        W = Convert.ToDouble(W_L[0]);
                        L = Convert.ToDouble(W_L[1]);
                    }
                    S03_CUT_H = S03_CUT_H + 1 / (Math.Max(Math.Floor(W / (SPONGE_LEN + 5)) * Math.Floor(L / (SPONGE_WID + 5)),
                        Math.Floor(L / (SPONGE_LEN + 5)) * Math.Floor(W / (SPONGE_WID + 5)))) * REQUIRE_QTY;
                }
                //S03_直刀机
                if (dt_Foam.Rows[j]["FOAM_PROCESS"].ToString().Contains("直刀"))
                {
                    S03_CUT_V = S03_CUT_V + (SPONGE_LEN + SPONGE_WID * 2) * REQUIRE_QTY;
                }
                //S03_冲床
                if (dt_Foam.Rows[j]["FOAM_PROCESS"].ToString().Contains("冲床"))
                {
                    S03_CUT_PUNCH = S03_CUT_PUNCH + REQUIRE_QTY;
                }
            }
            dr["PART_NO"] = Part_No;
            dr["S02_CUT_LEATHER"] = S02_CUT_LEATHER / 1000;
            dr["S02_CUT_FABRIC"] = S02_CUT_FABRIC / 1000;
            dr["S02_SEW"] = S02_SEW / 1000;
            dr["S02_CHECK_LEATHER"] = S02_CHECK_LEATHER / 1000000;
            dr["S03_CUT_H"] = S03_CUT_H;
            dr["S03_CUT_V"] = S03_CUT_V / 1000;
            dr["S03_CUT_PUNCH"] = S03_CUT_PUNCH;
            dtResult.Rows.Add(dr);

            switch (Feat_Name)
            {
                case "S02_裁布普通":
                    Feat_Value = Math.Round(S02_CUT_FABRIC / 1000, 2);
                    break;
                case "S02_裁布真皮":
                    Feat_Value = Math.Round(S02_CUT_LEATHER / 1000, 2);
                    break;
                case "S02_电车车缝":
                    Feat_Value = Math.Round(S02_SEW / 1000, 2);
                    break;
                case "S02_真皮检查":
                    Feat_Value = Math.Round(S02_CHECK_LEATHER / 1000000, 2);
                    break;
                case "S03_冲床":
                    Feat_Value = Math.Round(S03_CUT_PUNCH, 2);
                    break;
                case "S03_平刀机":
                    Feat_Value = Math.Round(S03_CUT_H, 2);
                    break;
                case "S03_直刀机":
                    Feat_Value = Math.Round(S03_CUT_V / 1000, 2);
                    break;
            }
            //临时判断
            if (Part_No == "3000000000")
                Feat_Value = 10;

            return dtResult;

        }

        //二个数取最大值
        private static double Maximun2(double Tw1, double Tw2)
        {
            if (Tw1 > Tw2)
            {
                return Tw1;
            }
            return Tw2;
        }
        //四个数取最大值
        public static double Maximum(double x, double y, double z, double h)
        {
            double maximumValue = x;
            if (y > maximumValue)
                maximumValue = y;
            if (z > maximumValue)
                maximumValue = z;
            if (h > maximumValue)
                maximumValue = h;
            return maximumValue;
        }
    }
}
